using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The StudentAttendanceTableDB class is responsible for interacting with the database to retrieve and store information
    /// about StudentAttendanceTable objects.
    /// </summary>
    public class StudentAttendanceTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of StudentAttendanceTable from the underlying datasource.
        /// </summary>
        /// <param name="attKey">The unique AttKey of the StudentAttendanceTable in the database.</param>
        /// <returns>An StudentAttendanceTable when the AttKey was found in the database, or null otherwise.</returns>
        public static StudentAttendanceTable GetItem(long attKey)
        {
            StudentAttendanceTable myStudentAttendanceTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@attKey", attKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAttendanceTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAttendanceTable;
            }
        }

        /// <summary>
        /// Gets an instance of StudentAttendanceTable from the underlying datasource.
        /// </summary>
        /// <param name="attKey">The unique AttKey of the StudentAttendanceTable in the database.</param>
        /// <returns>An StudentAttendanceTable when the AttKey was found in the database, or null otherwise.</returns>
        public static StudentAttendanceTable GetItemBySectionNo(int EduPeriodKey, int ClassRoomKey, Int16 DayCode, DateTime AttDate, Int16 SectionNo)
        {
            StudentAttendanceTable myStudentAttendanceTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTableSelectSingleItemBySectionNo", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@educationPeriodKey", EduPeriodKey);
                    myCommand.Parameters.AddWithValue("@classRoomKey", ClassRoomKey);
                    myCommand.Parameters.AddWithValue("@dayCode", DayCode);
                    myCommand.Parameters.AddWithValue("@attDate", AttDate);
                    myCommand.Parameters.AddWithValue("@sectionNo", SectionNo);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAttendanceTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAttendanceTable;
            }
        }

        /// <summary>
        /// Gets an instance of StudentAttendanceTable from the underlying datasource.
        /// </summary>
        /// <param name="attKey">The unique AttKey of the StudentAttendanceTable in the database.</param>
        /// <returns>An StudentAttendanceTable when the AttKey was found in the database, or null otherwise.</returns>
        public static StudentAttendanceTable GetItemByAttDate(int EduPeriodKey, int ClassRoomKey, Int16 DayCode, DateTime AttDate)
        {
            StudentAttendanceTable myStudentAttendanceTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTableSelectSingleItemByAttDate", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@educationPeriodKey", EduPeriodKey);
                    myCommand.Parameters.AddWithValue("@classRoomKey", ClassRoomKey);
                    myCommand.Parameters.AddWithValue("@dayCode", DayCode);
                    myCommand.Parameters.AddWithValue("@attDate", AttDate);
                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myStudentAttendanceTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myStudentAttendanceTable;
            }
        }
        
        

        /// <summary>
        /// Returns a list with StudentAttendanceTable objects.
        /// </summary>
        /// <returns>A generics List with the StudentAttendanceTable objects.</returns>
        public static StudentAttendanceTableList GetList()
        {
            StudentAttendanceTableList tempList = new StudentAttendanceTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a StudentAttendanceTable in the database.
        /// </summary>
        /// <param name="myStudentAttendanceTable">The StudentAttendanceTable instance to save.</param>
        /// <returns>The new AttKey if the StudentAttendanceTable is new in the database or the existing AttKey when an item was updated.</returns>
        public static int Save(StudentAttendanceTable myStudentAttendanceTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myStudentAttendanceTable.AttKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@attKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@attKey", myStudentAttendanceTable.AttKey);
                    }
                    if (myStudentAttendanceTable.DayCode <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@dayCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@dayCode", myStudentAttendanceTable.DayCode);
                    }
                    if (myStudentAttendanceTable.EducationPeriodKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@educationPeriodKey", myStudentAttendanceTable.EducationPeriodKey);
                    }
                    if (myStudentAttendanceTable.TeacherProfileKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@teacherProfileKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@teacherProfileKey", myStudentAttendanceTable.TeacherProfileKey);
                    }
                    if (myStudentAttendanceTable.SubjectKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@subjectKey", myStudentAttendanceTable.SubjectKey);
                    }
                    if (myStudentAttendanceTable.ClassRoomKey <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@classRoomKey", myStudentAttendanceTable.ClassRoomKey);
                    }
                    if (myStudentAttendanceTable.StartTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@startTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@startTime", myStudentAttendanceTable.StartTime);
                    }
                    if (myStudentAttendanceTable.EndTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@endTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@endTime", myStudentAttendanceTable.EndTime);
                    }
                    if (myStudentAttendanceTable.AttDate == null)
                    {
                        myCommand.Parameters.AddWithValue("@attDate", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@attDate", myStudentAttendanceTable.AttDate);
                    }
                    if (myStudentAttendanceTable.SectionNo <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@sectionNo", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@sectionNo", myStudentAttendanceTable.SectionNo);
                    }
                    if (myStudentAttendanceTable.Remark == string.Empty)
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", myStudentAttendanceTable.Remark);
                    }
                    if (myStudentAttendanceTable.UserID <= 0)
                    {
                        myCommand.Parameters.AddWithValue("@userID", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@userID", myStudentAttendanceTable.UserID);
                    }
                    //if (myStudentAttendanceTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", myStudentAttendanceTable.CreatedDateTime);
                    //}
                    //if (myStudentAttendanceTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentAttendanceTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a StudentAttendanceTable from the database.
        /// </summary>
        /// <param name="attKey">The AttKey of the StudentAttendanceTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(long attKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocStudentAttendanceTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@attKey", attKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the StudentAttendanceTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static StudentAttendanceTable FillDataRecord(IDataRecord myDataRecord)
        {
            StudentAttendanceTable myStudentAttendanceTable = new StudentAttendanceTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AttKey")))
            {
                myStudentAttendanceTable.AttKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("AttKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DayCode")))
            {
                myStudentAttendanceTable.DayCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("DayCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EducationPeriodKey")))
            {
                myStudentAttendanceTable.EducationPeriodKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("EducationPeriodKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherProfileKey")))
            {
                myStudentAttendanceTable.TeacherProfileKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherProfileKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SubjectKey")))
            {
                myStudentAttendanceTable.SubjectKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SubjectKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomKey")))
            {
                myStudentAttendanceTable.ClassRoomKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StartTime")))
            {
                myStudentAttendanceTable.StartTime = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("StartTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EndTime")))
            {
                myStudentAttendanceTable.EndTime = (TimeSpan)myDataRecord.GetValue(myDataRecord.GetOrdinal("EndTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AttDate")))
            {
                myStudentAttendanceTable.AttDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("AttDate"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SectionNo")))
            {
                myStudentAttendanceTable.SectionNo = myDataRecord.GetInt16(myDataRecord.GetOrdinal("SectionNo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                myStudentAttendanceTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserID")))
            {
                myStudentAttendanceTable.UserID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserID"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myStudentAttendanceTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myStudentAttendanceTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myStudentAttendanceTable;
        }
    }

}
